package cn.ljy.mes.service.impl;

import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.mes.dao.mapper.WmsMaterialBatchMapper;
import cn.ljy.mes.dao.mapper.WmsMaterialInventoryDetailMapper;
import cn.ljy.mes.dao.mapper.WmsMaterialInventoryDetailRelocationLogMapper;
import cn.ljy.mes.dao.mapper.WmsWarehouseSeatMapper;
import cn.ljy.mes.model.WmsMaterialInventoryDetailRelocationLog;
import cn.ljy.mes.model.query.WmsMaterialInventoryDetailRelocationLogQuery;
import cn.ljy.mes.service.WmsMaterialInventoryDetailRelocationLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

@Service
//@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor= MyRollBackException.class)
@Validated//表示开启sprint的校检框架，会自动扫描方法里的@Valid（@Valid注解一般写在接口即可）
@Slf4j
public class IWmsMaterialInventoryDetailRelocationLogService extends IBaseService<WmsMaterialInventoryDetailRelocationLog, WmsMaterialInventoryDetailRelocationLogQuery> implements WmsMaterialInventoryDetailRelocationLogService {
    @Autowired
    private WmsMaterialInventoryDetailMapper materialInventoryDetailMapper;
    @Autowired
    private WmsMaterialBatchMapper materialBatchMapper;
    @Autowired
    private WmsWarehouseSeatMapper wmsWarehouseSeatMapper;
    @Autowired
    private WmsMaterialInventoryDetailRelocationLogMapper materialInventoryDetailRelocationLogMapper;

    @Override
    protected void afterFindAll(List<WmsMaterialInventoryDetailRelocationLog> list) {
        for(WmsMaterialInventoryDetailRelocationLog relocationLog:list){
            //设置动作
            if(relocationLog.getSourceWarehouse()==null){
                relocationLog.setAction("入库");
                relocationLog.setActionDescription(String.format("目标库位（%s->%s->%s）入库：%d+%d->%d",
                        relocationLog.getWarehouseSeatOfTarget().getWarehouseArea().getWarehouse().getName(),
                        relocationLog.getWarehouseSeatOfTarget().getWarehouseArea().getName(),
                        relocationLog.getWarehouseSeatOfTarget().getName(),
                        relocationLog.getTargetInventory(),
                        relocationLog.getRelocationQuantity(),
                        relocationLog.getTargetInventory()+relocationLog.getRelocationQuantity()
                        ));
            }else if(relocationLog.getTargetWarehouse()==null){
                relocationLog.setAction("出库");
                relocationLog.setActionDescription(String.format("源库位（%s->%s->%s）出库：%d->%d",
                        relocationLog.getWarehouseSeatOfSource().getWarehouseArea().getWarehouse().getName(),
                        relocationLog.getWarehouseSeatOfSource().getWarehouseArea().getName(),
                        relocationLog.getWarehouseSeatOfSource().getName(),
                        relocationLog.getSourceInventory(),
                        relocationLog.getSourceInventory()-relocationLog.getRelocationQuantity()
                ));
            }else{
                relocationLog.setAction("移库");
                relocationLog.setActionDescription(String.format("源库位（%s->%s->%s）：%d->%d；目标库位（%s->%s->%s）：%d->%d",
                        relocationLog.getWarehouseSeatOfSource().getWarehouseArea().getWarehouse().getName(),
                        relocationLog.getWarehouseSeatOfSource().getWarehouseArea().getName(),
                        relocationLog.getWarehouseSeatOfSource().getName(),
                        relocationLog.getSourceInventory(),
                        relocationLog.getSourceInventory()-relocationLog.getRelocationQuantity(),
                        relocationLog.getWarehouseSeatOfTarget().getWarehouseArea().getWarehouse().getName(),
                        relocationLog.getWarehouseSeatOfTarget().getWarehouseArea().getName(),
                        relocationLog.getWarehouseSeatOfTarget().getName(),
                        relocationLog.getTargetInventory(),
                        relocationLog.getTargetInventory()+relocationLog.getRelocationQuantity()
                ));
            }
        }
    }
}
